﻿using System;

namespace Myotragus.Data.Tupples
{
	public class Tupple<TObject> : IEquatable<TObject>
		where TObject : class
	{
		#region Constants

		private static readonly Func<TObject, int> GetHashCodeMethod = EqualityFunctionsGenerator<TObject>
			.CreateGetHashCode();
		private static readonly Func<TObject, TObject, bool> EqualsMethod = EqualityFunctionsGenerator<TObject>
			.CreateEqualityComparer();

		#endregion

		#region Object Overrides

		public override bool Equals(object obj)
		{
			return Equals(obj as TObject);
		}

		public override int GetHashCode()
		{
			var @this = ((object)this) as TObject;
			
			if (@this == null)
				return 0 ;

			return GetHashCodeMethod(@this);
		}

		#endregion

		#region IEquatable<TEntity> Members

		public bool Equals(TObject other)
		{
			var @this = ((object)this) as TObject ;

			if (other == null || @this == null)
				return false ;

			return EqualsMethod(@this, other);
		}

		#endregion
	}
}
